//
//  ALPCustomViewController.m
//  AlpFM
//
//  Created by KING on 16/1/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPCustomViewController.h"
#import "ALPCustomHistoryViewController.h"
#import "ALPCustomCollectViewController.h"


#define COLOR_SELECTED [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1]
#define COLOR_NORMAL [UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1]
#define COLOR_BG [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1]
#define COLOR_NIGHT [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.8]

@interface ALPCustomViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIButton *favButton;
@property (nonatomic, retain) UIButton *hisButton;
@property (nonatomic, retain) UIScrollView *bigScrollView;
@property (nonatomic, retain) UIView *sliderView;
@end

@implementation ALPCustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    label.text = @"定制听";
    label.textAlignment = NSTextAlignmentCenter;
//    label.backgroundColor = CLEAR;
    self.navigationItem.titleView = label;
    
    //关闭自动根据导航条设置scrollView的frame
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    //大scroll
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 40)];
    self.bigScrollView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.bigScrollView];
    self.bigScrollView.contentSize = CGSizeMake(SCREEN_WIDTH *2,0);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;
    
    //小条
    self.sliderView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.favButton.frame.origin.x, self.favButton.frame.origin.y+40, self.favButton.frame.size.width, 2)];
    self.sliderView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.sliderView];
    
    //收藏
    ALPCustomCollectViewController *favVC = [[ALPCustomCollectViewController alloc] init];
    [self addChildViewController:favVC];
    favVC.view.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.bigScrollView addSubview:favVC.view];
    
    //历史
    ALPCustomHistoryViewController *hisVC = [[ALPCustomHistoryViewController alloc] init];
    hisVC.view.frame = CGRectMake(SCREEN_WIDTH, 40, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addChildViewController:hisVC];
    [self.bigScrollView addSubview:hisVC.view];
    
    [self createButtons];
}
#pragma mark - 创建按钮
- (void)createButtons{
    
    //收藏
    self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.favButton.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2.0, 40);
    [self.favButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    [self.favButton setTitle:@"收藏" forState:UIControlStateNormal];
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
//        self.favButton.backgroundColor = COLOR_NIGHT;
//    } else {
//        self.favButton.backgroundColor = COLOR_BG;
//    }
    self.favButton.titleLabel.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
    [self.view addSubview:self.favButton];
    [self.favButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //历史
    self.hisButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.hisButton.frame = CGRectMake(SCREEN_WIDTH / 2.0, 0, SCREEN_WIDTH / 2.0, 40);
    [self.hisButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.hisButton setTitle:@"历史" forState:UIControlStateNormal];
    self.hisButton.titleLabel.font = [UIFont systemFontOfSize:0.037*SCREEN_WIDTH];
    [self.view addSubview:self.hisButton];
    [self.hisButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"]) {
//        self.hisButton.backgroundColor = COLOR_NIGHT;
//    } else {
//        self.hisButton.backgroundColor = COLOR_BG;
//    }
}

#pragma mark - 点击按钮会滑到相应页
- (void)buttonAction:(UIButton *)btn{
    if (btn == self.favButton) {
        [self.bigScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    } else {
        [self.bigScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
        
    }
}

#pragma mark - 滑动就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.sliderView.frame = CGRectMake(self.bigScrollView.contentOffset.x / 2, self.favButton.frame.origin.y + 40, SCREEN_WIDTH/ 2, 2);
    
    NSInteger a = self.bigScrollView.contentOffset.x / SCREEN_WIDTH;
    
    switch (a) {
        case 0:
            
            [self.favButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.hisButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            
            break;
        case 1:
            [self.favButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.hisButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
